package com.jiepos.mpos.biz.code.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCodeListDao;
import com.jiepos.mpos.common.entity.App_Code_ListEntity;
import com.jiepos.mpos.common.service.SystemService;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 系统参数表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月12日下午8:19:33
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class AppCodeListService extends BaseService {
	
	@Autowired
	private AppCodeListDao appCodeListDao;
	@Autowired
	private SystemService systemService;
	
	/**
	 * 查询参数列表json分页数据
	 * @param pagination
	 * @param appCodeListEntity
	 * @return
	 */
	public Map getAppCodeList(Pagination pagination,App_Code_ListEntity appCodeListEntity){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append(" t1.code_uuid as uuid, t1.code_tp,t1.code_name,t1.code_val,t1.code_desc");
		sb.append(" from ");
		sb.append(Table.T_APP_CODE_LIST);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(appCodeListEntity.getCode_tp())) {
			sb.append( " and t1.code_tp like ? " );
			list.add("%" + appCodeListEntity.getCode_tp() + "%");
		}
		if (!StringUtils.isBlank(appCodeListEntity.getCode_name())) {
			sb.append( " and t1.code_name like ? " );
			list.add("%" + appCodeListEntity.getCode_name() + "%");
		}
		if (!StringUtils.isBlank(appCodeListEntity.getCode_val())) {
			sb.append( " and t1.code_val like ? " );
			list.add("%" + appCodeListEntity.getCode_val() + "%");
		}
		if (!StringUtils.isBlank(appCodeListEntity.getCode_desc())) {
			sb.append( " and t1.code_desc like ? " );
			list.add("%" + appCodeListEntity.getCode_desc() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return appCodeListDao.getPaginationMap(pagination, "t1", list); 
	}
	
	/**
	 * 保存参数信息
	 * @param appCodeListEntity
	 * @param map
	 */
	public void saveCode(App_Code_ListEntity appCodeListEntity,Map map) {	
		
		if (!StringUtils.isBlank(appCodeListEntity.getCode_uuid())) {
			App_Code_ListEntity newAppCodeListEntity=appCodeListDao.findByParam("code_uuid", appCodeListEntity.getCode_uuid());
			newAppCodeListEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
			newAppCodeListEntity.setCode_tp(appCodeListEntity.getCode_tp().toUpperCase());
			newAppCodeListEntity.setCode_name(appCodeListEntity.getCode_name());
			newAppCodeListEntity.setCode_val(appCodeListEntity.getCode_val());
			newAppCodeListEntity.setCode_desc(appCodeListEntity.getCode_desc());
			newAppCodeListEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));	
			newAppCodeListEntity.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("code_uuid");
			noChangeList.add("rec_crt_ts");
			appCodeListDao.update(newAppCodeListEntity,noChangeList);
		} else {
			appCodeListEntity.setCode_tp(appCodeListEntity.getCode_tp().toUpperCase());
			appCodeListEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));			
			appCodeListEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			appCodeListEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			appCodeListEntity.setCode_uuid(IDFactoryUtil.getUUID());			
			appCodeListEntity.setRec_st(SystemConstant.NORMAL_STATUS);
			appCodeListEntity.setLast_oper_in(SystemConstant.OPER_ADD);
			appCodeListDao.save(appCodeListEntity);
		}
		
		systemService.updateRedis(SystemConstant.CACHE_CODELIST, systemService.getAllCodeList());
	}
	
	/**
	 * 根据记录id查询参数信息实体对象
	 * @param code_uuid
	 * @return
	 */
	public App_Code_ListEntity getCodeById(String code_uuid) {
		if (StringUtils.isBlank(code_uuid) || code_uuid.equals("undefined")) {
			return new App_Code_ListEntity();
		}
		return appCodeListDao.findByParam("code_uuid", code_uuid);
	}

	/**
	 * 根据记录id集合删除数据
	 * @param code_uuids
	 */
	public void deleteCodeByIds(String code_uuids) {
		appCodeListDao.deleteByStatus("code_uuid",code_uuids);
		systemService.updateRedis(SystemConstant.CACHE_CODELIST, systemService.getAllCodeList());
	}
	
	/**
	 * 校验系统参数
	 * @param code_uuid
	 * @return
	 */
	public String checkCode(App_Code_ListEntity appCodeListEntity){
		StringBuffer sb = getInitSql("t1");
		sb.append(",t1.code_uuid as uuid, t1.code_tp,t1.code_name,t1.code_val,t1.code_desc");
		sb.append(" from ");
		sb.append(Table.T_APP_CODE_LIST);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(appCodeListEntity.getCode_tp())) {
			sb.append( " and t1.code_tp = ? " );
			list.add(appCodeListEntity.getCode_tp().toUpperCase());
		}		
		if (!StringUtils.isBlank(appCodeListEntity.getCode_val())) {
			sb.append( " and t1.code_val = ? " );
			list.add(appCodeListEntity.getCode_val());
		}
		if (!StringUtils.isBlank(appCodeListEntity.getCode_uuid())) {
			sb.append( " and t1.code_uuid != ? " );
			list.add(appCodeListEntity.getCode_uuid());
		}
		List result=appCodeListDao.queryForList(sb.toString(), list);
		if(result.size()==0||result==null){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}

	/**
	 * 根据code类型和code值 得到对象
	 * @param code_tp
	 * @param code_val
	 * @return
	 */
	public App_Code_ListEntity findByCondition(String code_tp,String code_val){
		Object[] param1 = new Object[]{"code_tp","code_val","rec_st"};
		Object[] param2 = new Object[]{code_tp,code_val,SystemConstant.REC_ST_1};
		return appCodeListDao.findByParamses(param1,param2);
	}
}
